package com.ruicar.afs.cloud.batch.frame.ws;

import com.ruicar.afs.cloud.batch.frame.logger.WsMonitorHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/batchMonitor")
@Component
@Slf4j
public class MonitorWebSocketHandle {
    /**
     * 新的WebSocket请求开启
     */
    @OnOpen
    public void onOpen(Session session) {
        try {
            WsMonitorHelper.addSession(session);
        } catch (Exception e) {
            log.error("",e);
            WsMonitorHelper.removeSession(session);
        }
    }

    /**
     * WebSocket请求关闭
     */
    @OnClose
    public void onClose(Session session) {
        try {
            WsMonitorHelper.removeSession(session);
        } catch (Exception e) {
        }
    }

    @OnError
    public void onError(Throwable thr) {
        log.error("",thr);
    }
}
